<template>
  <div id="admin" class="container">
    <el-button
      type="primary"
      class="add five"
      style="margin-bottom: 20px"
      icon="el-icon-plus"
      size="small"
      @click="open"
    >
      新增会员等级
    </el-button>
    <el-table
      :data="tableData"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="120px" />
      <el-table-column prop="title" label="标题" />
      <el-table-column label="图标">
        <template slot-scope="scope">
          <el-avatar
            v-if="scope.row.icon"
            shape="square"
            size="medium"
            :src="scope.row.icon.url"
          />
        </template>
      </el-table-column>
      <el-table-column prop="min_score" label="积分最低值" />
      <el-table-column prop="max_score" label="积分最低值" />
      <el-table-column prop="sort" label="排序" />
      <el-table-column prop="create_time" label="添加时间" />
      <el-table-column prop="update_time" label="修改时间" />
      <el-table-column label="操作" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button class="edit" size="mini" @click="editUserGrade(scope.row)">
            编辑
          </el-button>
          <el-button class="dele" size="mini" @click="delUserGrade(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <BouncedDialog
      :title="adminForm.id ? '编辑会员等级' : '添加会员等级'"
      :dialog-show.sync="dialogAdmin"
      :width="'50'"
      class="dialog"
    >
      <el-form
        ref="adminForm"
        :model="adminForm"
        inline
        :rules="adminRules"
        label-width="120px"
      >
        <el-form-item label="标题：" prop="title">
          <el-input v-model="adminForm.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="等级图标：" prop="icon">
          <div class="attachment_imgBox" @click="openList('icon')">
            <img v-if="adminForm.icon" :src="iconUrl" class="attachmentImg" />
            <i v-else class="el-icon-camera" />
          </div>
          <Attachment
            ref="attachment"
            :dialog-show.sync="dialogShow"
            :attachment-type="attachmentType"
            @func="getData"
          />
        </el-form-item>
        <el-form-item label="积分最小值：" prop="min_score">
          <el-input
            v-model="adminForm.min_score"
            placeholder="请输入积分最小值"
          />
        </el-form-item>
        <el-form-item label="积分最大值：" prop="max_score">
          <el-input
            v-model="adminForm.max_score"
            placeholder="请输入积分最大值"
          />
        </el-form-item>
        <el-form-item label="排序权重：" prop="sort">
          <el-input v-model="adminForm.sort" />
        </el-form-item>
        <div class="btn">
          <el-button @click="dialogAdmin = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
      </el-form>
    </BouncedDialog>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total < page.size ? true : false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import userApi from '@/api/users.js'
import attachmentApi from '@/api/system/attachment'
export default {
  data() {
    return {
      attachmentLimit: 10,
      dialogShow: false,
      attachmentData: [],
      attachmentType: '',
      iconUrl: '',
      tableData: [],
      searchForm: {},
      dialogAdmin: false,
      options: [], // 角色
      name: '', // 暂存会员等级姓名
      adminForm: {
        title: '',
        icon: '',
        min_score: '',
        max_score: '',
        sort: 0,
        id: ''
      }, // 新增会员等级表单
      adminRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        min_score: [
          { required: true, message: '请输入积分最小值', trigger: 'blur' }
        ],
        max_score: [
          { required: true, message: '请输入积分最大值', trigger: 'blur' }
        ]
      },
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      }, // 分页器
      val: {}
    }
  },
  created() {
    this.getList()
  },
  updated() {},
  methods: {
    getData(data) {
      if (data.length > 1) {
        this.$message.error('最多只能选择一张图片')
      } else {
        if (this.uploadType == 'icon') {
          this.adminForm.icon = data[0].id
          this.iconUrl = data[0].url
          console.log(
            this.adminForm.icon,
            this.iconUrl,
            '选中的图片>>>>>>>>>>>>'
          )
        }
        this.dialogShow = false
      }
    },
    // 打开附件弹窗
    openList(val) {
      this.dialogShow = true
      this.attachmentType = 2
      this.$nextTick(() => {
        this.$refs.attachment.getAttachmentList()
        this.$refs.attachment.$refs.AttachmentCate.getCateList(0)
        console.log(this.$refs.attachment.$refs.AttachmentCate)
      })
      this.uploadType = val
      console.log(val)
    },
    open() {
      this.dialogAdmin = true
      this.adminForm = {
        title: '',
        desc: '',
        id: ''
      }
    },
    editUserGrade(row) {
      this.dialogAdmin = true
      this.adminForm = row
      if (row.icon) {
        this.adminForm.icon = row.icon.id
        this.iconUrl = row.icon.url
      }
    },
    delUserGrade(row) {
      this.$confirm('此操作将永久删除此会员等级, 是否继续?', '删除会员等级', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          userApi.delUserGrade({ id: row.id }).then(res => {
            if (res.code == 0) {
              this.$message.success(res.msg)
              this.getList()
            } else {
              this.$message.error(res.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取列表
    getList() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size
      this.val.account = this.searchForm.account
      userApi.userGradeList(this.val).then(res => {
        this.tableData = res.data
        this.page.total = res.count
      })
    },

    // 提交
    submit() {
      this.$refs.adminForm.validate(valid => {
        if (valid) {
          if (!this.adminForm.id) {
            userApi
              .userGradeAdd({
                ...this.adminForm
              })
              .then(res => {
                if (res.code === 0) {
                  this.$message.success(res.msg)
                  this.dialogAdmin = false
                  this.getList()
                } else {
                  this.$message.error(res.msg)
                }
              })
          } else {
            userApi.userGradeEdit({ ...this.adminForm }).then(res => {
              if (res.code === 0) {
                this.$message.success(res.msg)
                this.dialogAdmin = false
                this.getList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          this.$message.warning('请填写必要信息')
        }
      })
    },

    // 搜索
    search() {
      this.$refs.searchForm.validate(valid => {
        this.page.currentPage = 1
        this.getList()
      })
    },

    // 重置搜索框
    reset(searchForm) {
      this.searchForm = {
        account: ''
      }
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getList()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getList()
    }
  }
}
</script>
<style lang="scss">
#admin {
  .el-form-item {
    width: 100%;
  }
  .el-form-item__content {
    width: calc(100% - 130px);
    .el-select {
      width: calc(100%);
    }
  }
}
</style>
<style lang="scss" scoped>
.el-avatar {
  background: #eff0f1;
}
.tabs {
  padding: 15px 10px;
}

#admin {
  background-color: #fff;
}

.el-table {
  margin-bottom: 20px;
}
.search {
  background-color: #fff;
  padding: 15px 10px;
  margin-bottom: 10px;
  .el-form-item {
    margin-bottom: 0;
  }
  .el-button {
    font-size: 14px;
  }
}
.btn {
  text-align: center;
}
</style>
